<template>
  <view>
    <view class="goods-list">
      <view class="tips" v-if="isempty">
        <image src="../../static/sousuo.jpg" mode="heightFix" style="width: 40%;"></image>
        <view class="a">该商品缺货中</view>
        <view class="b">“看看其他商品吧”</view>
      </view>
      <view v-for="(item,i) in goodsList" :key="i">
        <my-goods :goods="item" ></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryobj: {
          query: "",
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        pagetotal:0,
        goodsList: [],
        total: 0,
        isloading:false,
        isempty:false
      };
    },
    onLoad(options) {
      this.queryobj.query = options.query || ''
      this.queryobj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      gotoGoodsDetail(item){
        uni.navigateTo({
          url: "/subpkg/good_detail/good_detail?goods_id=" + item.goods_id
        })
      },
      async getGoodsList(cb) {
        this.isloading=true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryobj)
        this.isloading=false
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goodsList = [...this.goodsList,...res.message.goods]
        if(this.goodsList.length==0){
          this.isempty=true
        }
        this.total = res.message.total
        this.pagetotal=parseInt(this.total/10)+1
        cb&&cb()
      }
    },
    onReachBottom() {
      if(this.queryobj.pagenum>=this.pagetotal) return uni.$showMsg('没有更多商品啦~')
      if(this.isloading) return
      this.queryobj.pagenum++
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryobj.pagenum=Math.floor(Math.random()*this.pagetotal)
      this.total=0
      this.goodsList=[]
      this.isloading=false
      this.getGoodsList(()=>{
        uni.stopPullDownRefresh()
      }) 
    }
  }
</script>

<style lang="scss">
  .tips{
    text-align: center;
    margin-top: 15rpx;
    image{
     transform:  scale(0.5);
    }
    .a{
      font-size: 18px;
      margin-top: -55px;
      margin-bottom: 6rpx;
    }
    .b{
      font-size: 12px;
      color: gray;
    }
  }
</style>
